//
//  CoinTransViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/20.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
let timerName = "transTimer"
class CoinTransViewModel: BaseViewModel {

    @objc dynamic var totalMoney:String = "0"
    @objc dynamic var refreshBool = false //监听的状态
    var model:AssetModel = AssetModel() //传递的model
    var selectIndex:Int = 0
    var dataModel:CoinTransModel = CoinTransModel()
    var type:CoinAccountType?

    lazy var legalRequest: CashFlowRequest = {
        let api = CashFlowRequest()
        api.type = type
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    override init(controller: BaseViewController) {
         super.init(controller: controller)
        
    }
    
    
    /// 获取资金流水
    func archiveInformation() {
        self.request(request: legalRequest, success: {[weak self] (result) in
            let dic:[String:Any]? = result != nil ? result : nil
            if dic == nil {
                return
            }
            if dic?["data"] as? [String : Any] == nil  {
                return
            }
            self?.dataModel = CoinTransModel(JSON: (dic?["data"] as? [String : Any])!) != nil ? CoinTransModel(JSON: (dic?["data"] as? [String : Any])!)!  : CoinTransModel()
            self?.refreshBool = true
            
        }) { (reponse) in
            
        }
    }
    
    /// 计算全部资产
    ///
    ///totolMoeny 全部资金
    /// - Parameter model: 资产
   func archiveAllMoney(model:AssetModel,totolMoeny:String) -> String{
        let totalCoin = totolMoeny
        let money =  AppContext.default.archiveLegalPrice(coinKind: AppCacheInfo.default.currency, legalKind: model.coinCode.count != 0 ? (model.coinCode) : "")
        let currentMoney = CYCalculate.cy_multiplying(firstNumber: money, lastNumber: model.currentAmount, digit: 2)
        return CYCalculate.cy_add(firstNumber: totalCoin, lastNumber: currentMoney, digit: 2)
    }
    
    func archiveMoney()  {
       totalMoney = "0"
       totalMoney = (self.archiveAllMoney(model: model, totolMoeny: totalMoney))
    }
    
    func checkCurrentModel()  {
        if type == .legalType {
            for data in APPAssetInfo.default.legalAssetArr {
                if data.coinCode == self.model.coinCode {
                    self.model = data
                    return
                }
            }
            
        }else if type == .coinType {
            for data in APPAssetInfo.default.coinAssetArr {
                if data.coinCode == self.model.coinCode {
                    self.model = data
                    return
                }
            }
            
        }
    }
    
    func archiveModelIndex() {
        if type == .coinType {
            selectIndex = APPAssetInfo.default.coinAssetArr.index(of: model) ?? 0
        }
    }
    
    func openTimer() {
        CYGCDTimer.shared.scheduledDispatchTimer(WithTimerName: timerName, timeInterval: 60, queue: .main, repeats: true) {
            self.archiveMoney()
        }
    }
    func closeTimer()  {
        CYGCDTimer.shared.cancleTimer(WithTimerName: timerName)
    }
}
extension CoinTransViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? CashFlowRequest {
            pramas.updateValue(1, forKey: "pageNo")
            pramas.updateValue(3, forKey: "pageSize")
            pramas.updateValue(model.coinCode, forKey: "coinCode")
//            pramas.updateValue(0, forKey: "handleType")
        }
        return pramas
    }
    
    
    
}
